<template>
    <div class="main-right">
        <div class="rt-echarts">
            <div class="echartTitles" @click="titleClick('exportType')">
                <div class="title-name">
                    专家服务
                </div>
            </div>
            <div class="dataCharts">
                <div class="title">专家人数（人）:<span class="nums">&nbsp;&nbsp;&nbsp;{{ personNum }}</span></div>
                <div ref="initCharts" style="width: 100%;flex: 1;"></div>
            </div>
        </div>
        <div class="rc-echarts">
            <div class="echartTitles" @click="titleClick('plantType')">
                <div class="title-name" >
                    种植品种
                </div>
            </div>
            <div class="init-boxs">
                <div class="title">苹果种植面积品种</div>
                <div  ref="bingCharts" style="width: 100%;flex: 1;"></div>
            </div>
        </div>
        <div class="rb-echarts">
            <div class="echartTitles" @click="titleClick('saleType')">
                <div class="title-name">
                    销售产量
                </div>
            </div>
            <div class="initScroll"  ref="initScroll">
            </div>
        </div>
    </div>
</template>
<script>
import * as echarts from "echarts";
export default {
    props: ['rightData'],
    data() {
        return {
            firstData: [],
            secondData: [],
            thirdData: [],
            allNum: 0,
            personNum: 0,
            exportType:{
                propOption:[],
                title:"专家服务",
                times:false,
            },
            plantType:{
                propOption:[
                  {name:'镇',value:"镇"},
                  {name:'果园',value:"果园"},
                ],
                title:"种植面积",
                times:false,
            },
            saleType:{
                propOption:[
                  {name:'本周',value:"周"},
                  {name:'一个月',value:"月"},
                  {name:'半年',value:"半年"},
                ],
                title:"价格趋势",
                times:true,
            },
        }
    },
    watch: {
        rightData: {
            handler(newValue) {
                if (!newValue) return
                if(newValue.top.length>0){
                    this.firstData = newValue.top.map(v => {
                    this.personNum += v.num
                    return {
                        name: v.type,
                        value: v.num
                    }
                })
                }
                if(newValue.center.length>0){
                    newValue.center.forEach((v, i) => { 
                      let data= {
                            value: Number(v.area),
                            name: v.plantName,
                            unit:v.unit
                        }
                        this.allNum += Number(v.area)
                        this.secondData.push(data)
                    })
                }
                if(newValue.bottom.length>0){
                    this.thirdData =  newValue.bottom.map(v=>{
                     return {
                        name:v.variety,
                        value:Number(v.val)
                     }
                    })
                }
                
                this.$nextTick(() => {
                    this.firstChart()
                    this.secondChart()
                    this.thirdChart()
                })
            },
            deep: true,  // 深度监听
        }
    },
    methods: {
        firstChart() {
            let myChart = echarts.getInstanceByDom(this.$refs.initCharts);
            if (myChart == undefined) {
             myChart = echarts.init(this.$refs.initCharts);
            }
            myChart.clear()
            let option = this.getPie3D(
                this.firstData,
                0.7
            );

            if (option && typeof option === "object") {
                myChart.setOption(option);
            }
        },
        secondChart() { 
            let myChart = echarts.getInstanceByDom(this.$refs.bingCharts);
            if (myChart == undefined) {
             myChart = echarts.init(this.$refs.bingCharts);
            }
            myChart.clear()
            let echartData = this.secondData
            let option = {
                backgroundColor: '',
                title: {
                    text: this.allNum,
                    textStyle: {
                        color: 'rgba(68, 197, 177, 1)',
                        fontSize: 15
                    },
                    left: 'center',
                    top: '40%',
                    subtext: '总面积(万亩)',
                    subtextStyle: {
                        color: 'rgba(68, 197, 90, 1)',
                        fontSize: 12,
                        fontWeight: 'normal'
                    }
                },
                series: [{
                    type: 'pie',
                    radius: ['50%', '80%'],
                    center: ['50%', '50%'],
                    label: {
                        show: true,
                        position: 'outside',
                        formatter: "{d|{b}} \n {b|{c}万亩}",
                        rich: {
                        d: {
                        color: '#fff',
                        fontSize: 12,
                        lineHeight: 14,
                        height: 14
                        },
                        b: {
                        color: "#D3EBFF",
                        fontSize: 12,
                        lineHeight: 14,
                        height: 14,
                        align: 'left',
                        }
              },
                    },
                    labelLine: {
                        length: 5,
                        length2: 20,
                        lineStyle: {
                            color: '#e6e6e6',
                        },
                    },
                    data: echartData.map((item, index) => {
                        return {
                            ...item,
                            label: {
                                color: item.color
                            }
                        }
                    })
                }
                ]
            }
            if (option && typeof option === "object") {
                myChart.setOption(option);
            }
        },
        thirdChart() {
            let myChart = echarts.getInstanceByDom(this.$refs.initScroll);
            if (myChart == undefined) {
             myChart = echarts.init(this.$refs.initScroll);
            }
            myChart.clear()
            let datas = this.thirdData
            let names = datas.map(v => {
                return v.name
            })
            let value = datas.map(v => {
                return v.value
            })
            let option = {
                backgroundColor: '',
                grid: {
                    top: '25%',
                    left: '8%',
                    right: '3%',
                    bottom: '10%',
                    containLabel: true,
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    },
                    formatter: function (params) {
                        return params[0].name + ' : ' + params[0].data+'万吨';
                    }
                },
                xAxis: {
                    type: 'category',
                    data: names,
                    splitLine: {
                        show: false,
                    },
                    axisLabel: {
                        color: "#A1BADF",
                        fontSize: 13,
                        interval: 'auto', // x轴间距
                        color: '#FFFFFF',
                    },
                    axisTick: {
                        show: false
                    },
                    axisLine: {
                        lineStyle: {
                            color: 'rgba(152, 212, 255, .1)',
                        }
                    },
                },
                yAxis: [{
                    type: 'value',
                    name: "销售（万吨）",
                    nameGap: '30',
                    min: 0,
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                        show: true,
                        fontSize: 13,
                        color: "#ffffff",
                    },
                    axisLine: {
                        show: true,
                        lineStyle: {
                            color: '#00CCFF',
                            width: 1,
                        }
                    },
                    splitLine: {
                        lineStyle: {
                            color: '#00CCFF',
                            width: 1,
                            type: 'dashed'
                        }
                    },
                    nameTextStyle: {
                        color: "#ffffff",
                        fontWeight: 400,
                        fontSize: 13,
                    },
                },
                ],
                series: [
                    {
                    data: value,
                    type: "bar", // 柱图
                    barMaxWidth: "auto",
                    barWidth: 15,
                    itemStyle: {
                        color: {
                            x: 0,
                            y: 0,
                            x2: 0,
                            y2: 1,
                            type: "linear",
                            global: false,
                            colorStops: [{
                                offset: 0,
                                color: "rgba(0, 255, 168, 0.8)"
                            }, {
                                offset: 0.5,
                                color: "rgba(44, 163, 94, 0.5)"
                            }, {
                                offset: 1,
                                color: "rgba(0, 255, 168, 0.8)"
                            }]
                        }
                    }
                }, {
                    data: [1, 1, 1, 1, 1],
                    type: "pictorialBar", // 底部
                    barMaxWidth: "15",
                    symbolOffset: [0, "50%"],
                    symbolSize: [15, 3]
                }, {
                    data: value,
                    type: "pictorialBar",  // 顶部
                    barMaxWidth: "15",
                    symbolPosition: "end",
                    symbolOffset: [0, "-50%"],
                    symbolSize: [15, 6],
                    zlevel: 2
                }, 
                ]
            };
            if (option && typeof option === "object") {
                myChart.setOption(option);
            }
        },
        // 生成扇形的曲面参数方程，用于 series-surface.parametricEquation
        getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, h) {
            // 计算
            let midRatio = (startRatio + endRatio) / 2;

            let startRadian = startRatio * Math.PI * 2;
            let endRadian = endRatio * Math.PI * 2;
            let midRadian = midRatio * Math.PI * 2;

            // 如果只有一个扇形，则不实现选中效果。
            // if (startRatio === 0 && endRatio === 1) {
            //     isSelected = false;
            // }
            isSelected = false;
            // 通过扇形内径/外径的值，换算出辅助参数 k（默认值 1/3）
            k = typeof k !== 'undefined' ? k : 1 / 3;

            // 计算选中效果分别在 x 轴、y 轴方向上的位移（未选中，则位移均为 0）
            let offsetX = isSelected ? Math.sin(midRadian) * 0.1 : 0;
            let offsetY = isSelected ? Math.cos(midRadian) * 0.1 : 0;

            // 计算高亮效果的放大比例（未高亮，则比例为 1）
            let hoverRate = isHovered ? 1.05 : 1;

            // 返回曲面参数方程
            return {
                u: {
                    min: -Math.PI,
                    max: Math.PI * 3,
                    step: Math.PI / 32,
                },

                v: {
                    min: 0,
                    max: Math.PI * 2,
                    step: Math.PI / 20,
                },

                x: function (u, v) {
                    if (u < startRadian) {
                        return offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    if (u > endRadian) {
                        return offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
                },

                y: function (u, v) {
                    if (u < startRadian) {
                        return offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    if (u > endRadian) {
                        return offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
                },

                z: function (u, v) {
                    if (u < -Math.PI * 0.5) {
                        return Math.sin(u);
                    }
                    if (u > Math.PI * 2.5) {
                        return Math.sin(u) * h * 0.1;
                    }
                    return Math.sin(v) > 0 ? 1 * h * 0.1 : -1;
                },
            };
        },

        // 生成模拟 3D 饼图的配置项
        getPie3D(pieData, internalDiameterRatio) {
            let series = [];
            let sumValue = 0;
            let startValue = 0;
            let endValue = 0;
            let legendData = [];
            let k =
                typeof internalDiameterRatio !== 'undefined'
                    ? (1 - internalDiameterRatio) / (1 + internalDiameterRatio)
                    : 1 / 3;

            // 为每一个饼图数据，生成一个 series-surface 配置
            for (let i = 0; i < pieData.length; i++) {
                sumValue += pieData[i].value;

                let seriesItem = {
                    name: typeof pieData[i].name === 'undefined' ? `series${i}` : pieData[i].name,
                    type: 'surface',
                    parametric: true,
                    wireframe: {
                        show: false,
                    },
                    pieData: pieData[i],
                    pieStatus: {
                        selected: false,
                        hovered: false,
                        k: 1 / 10,
                    },
                };

                if (typeof pieData[i].itemStyle != 'undefined') {
                    let itemStyle = {};

                    typeof pieData[i].itemStyle.color != 'undefined' ? (itemStyle.color = pieData[i].itemStyle.color) : null;
                    typeof pieData[i].itemStyle.opacity != 'undefined'
                        ? (itemStyle.opacity = pieData[i].itemStyle.opacity)
                        : null;

                    seriesItem.itemStyle = itemStyle;
                }
                series.push(seriesItem);
            }
            // 使用上一次遍历时，计算出的数据和 sumValue，调用 getParametricEquation 函数，
            // 向每个 series-surface 传入不同的参数方程 series-surface.parametricEquation，也就是实现每一个扇形。
            for (let i = 0; i < series.length; i++) {
                endValue = startValue + series[i].pieData.value;
                series[i].pieData.startRatio = startValue / sumValue;
                series[i].pieData.endRatio = endValue / sumValue;
                series[i].parametricEquation = this.getParametricEquation(
                    series[i].pieData.startRatio,
                    series[i].pieData.endRatio,
                    false,
                    false,
                    k,
                    series[i].pieData.value
                );
                startValue = endValue;
                legendData.push(series[i].name);
            }
            // 准备待返回的配置项，把准备好的 legendData、series 传入。
            let option = {
                tooltip: {
                    formatter: (params) => {
                        if (params.seriesName !== "mouseoutSeries") {
                            return `${params.seriesName
                                }<br/><span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${params.color
                                };"></span>${option.series[params.seriesIndex].pieData.value + "人"
                                }`;
                        }
                        return "";
                    },
                },
                legend: {
                    show: true,
                    orient: "vertical",
                    data: pieData,
                    top: "center",
                    itemGap: 14,
                    itemHeight: 8,
                    itemWidth: 17,
                    right: "4%",
                    textStyle: {
                        color: "#fff",
                    },
                    fontSize: 12,
                },
                label: {
                    show: true,
                    position: "outside",
                    formatter: "{b}",
                    color: "#fff",
                    fontSize: "14px",
                },
                xAxis3D: {
                    min: -1,
                    max: 1,
                },
                yAxis3D: {
                    min: -1,
                    max: 1,
                },
                zAxis3D: {
                    min: -1,
                    max: 1,
                },
                grid3D: {
                    viewControl: {
                        autoRotate: true,
                        distance: 180,
                        alpha: 25,
                        beta: 200,
                    },
                    width: '80%',
                    show: false,
                    boxHeight: 3,
                },
                series: series,
            };
            return option;
        },
        //title点击
        titleClick(e){
            this.$emit('titleHandler',this[e])
        } 
    }
}
</script>
<style lang="less" scoped>
.main-right {
    width: 25%;
    height: 100%;
    display: flex;
    flex-direction: column;

    .rt-echarts {
        width: 100%;
        flex: 1;
        display: flex;
        flex-direction: column;

        .dataCharts {
            flex: 1;
            width: 100%;
            display: flex;
            flex-direction: column;

            .title {
                font-size: 14px;
                font-weight: 500;
                color: #71F286;
                width: 100%;
                height: 50px;
                display: flex;
                align-items: center;
                padding-left: 60px;

                .nums {
                    font-size: 24px;
                    font-weight: bold;
                    color: #74FFEA;
                }
            }
        }
    }

    .rc-echarts {
        width: 100%;
        flex: 1;
        display: flex;
        flex-direction: column;

        .init-boxs {
            width: 100%;
            flex: 1;
            display: flex;
            flex-direction: column;

            .title {
                font-size: 14px;
                font-weight: 500;
                color: #71F286;
                width: 100%;
                height: 50px;
                display: flex;
                align-items: center;
                padding-left: 60px;

                .nums {
                    font-size: 24px;
                    font-weight: bold;
                    color: #74FFEA;
                }
            }
        }


    }

    .rb-echarts {
        width: 100%;
        flex: 1;
        display: flex;
        flex-direction: column;

        .initScroll {
            width: 100%;
            flex: 1;
        }
    }

}


</style>